<?php
/**
 * Author: xx.com
 * Date: 2015-09-09
 */
namespace Admin\Controller;
use Admin\Logic\GoodsLogic;
use Think\AjaxPage;
use Think\Page;

class ShopGoodsController extends BaseController {

    /**
     *  渠道商品列表
     */
    public function shopGoodsList(){
        $this->assign('shop_id', I('shop_id', 0, 'intval'));
        $is_shop = 1;
        $this->assign('is_shop', $is_shop);
        $this->display();
    }

    public function ajaxShopGoodsList(){
        $shop_id = I('shop_id', 0, 'intval');
        $shop_info = M('shop')->where(array('id'=>$shop_id))->find();
        $where = ' 1 = 1 '; // 搜索条件
        I('intro')    && $where = "$where and ".I('intro')." = 1" ;
        I('brand_id') && $where = "$where and brand_id = ".I('brand_id') ;
        (I('is_on_sale') !== '') && $where = "$where and is_on_sale = ".I('is_on_sale') ;
        $cat_id = I('cat_id');
        // 关键词搜索
        $key_word = I('key_word') ? trim(I('key_word')) : '';
        if($key_word)
        {
            $where = "$where and (goods_name like '%$key_word%' or goods_sn like '%$key_word%')" ;
        }

        if($cat_id > 0)
        {
            $grandson_ids = getCatGrandson($cat_id);
            $where .= " and cat_id in(".  implode(',', $grandson_ids).") "; // 初始化搜索条件
        }

        $is_shop = 1;
        $shopGoodsModel = M('shop_goods');

            //$where .= " and shop_id = {$this->shop_info['shop_id']}";

//             var_dump($this->shop_info);
            // var_dump($where);

            $model = M('Goods');
            $where .=  ' and shop_goods_type = ' .$shop_info['goods_type'];

            $count = $model->where($where)->count();
            $Page = new AjaxPage($count, 10);
            /**  搜索条件下 分页赋值
             * foreach($condition as $key=>$val) {
             * $Page->parameter[$key]   =   urlencode($val);
             * }
             */
            $show = $Page->show();
            $order_str = "`{$_POST['orderby1']}` {$_POST['orderby2']}";
            $goodsList = $model->where($where)->order($order_str)->limit($Page->firstRow . ',' . $Page->listRows)->select();

            foreach($goodsList as $key => $val){
                $shop_goods = $shopGoodsModel->where(array('shop_id'=>$shop_id, 'goods_id'=>$val['goods_id']))->find();
                if($shop_goods){
                    $goodsList[$key]['shop_store_count'] = $shop_goods['store_count'];
                    $goodsList[$key]['shop_on_sale'] = $shop_goods['shop_on_sale'];
                }else{
                    $goodsList[$key]['shop_store_count'] = 0;
                    $goodsList[$key]['shop_on_sale'] = 0;
                }
            }

        $catList = D('goods_category')->select();
        $catList = convert_arr_key($catList, 'id');
        $this->assign('catList',$catList);
        $this->assign('goodsList',$goodsList);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('is_shop', $is_shop);
        $this->display();
    }
    /**
     *  渠道商品列表
     */
    public function ajaxShopGoodsList_0(){
        $where['goods_id'] = I('goods_id', 0, 'intval');
        $model = M('shop_goods');
        $count = $model->where($where)->count();
        $Page = new AjaxPage($count, 10);
        $show = $Page->show();
        $order_str = "id DESC";
        $shopGoodsList = $model->where($where)->order($order_str)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $shopModel = M('shop');
        foreach($shopGoodsList as $key => $val){
             $shopInfo = $shopModel->field('shop_name, shop_user, shop_tel')->where(array('id'=>$val['shop_id']))->find();
             if($shopInfo){
                 $shopGoodsList[$key]['shop_name'] = $shopInfo['shop_name'];
                 $shopGoodsList[$key]['shop_user'] = $shopInfo['shop_user'];
                 $shopGoodsList[$key]['shop_tel'] = $shopInfo['shop_tel'];
             }else{
                 $shopGoodsList[$key]['shop_name'] = '';
                 $shopGoodsList[$key]['shop_user'] = '';
                 $shopGoodsList[$key]['shop_tel'] = '';
             }
        }
        $this->assign('shopGoodsList',$shopGoodsList);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    public function updateStore(){
        $goods_id = I('goods_id', 0, 'intval');
        if($goods_id <= 0){
            $this->returnInfo(-1,"商品信息有误");
        }
        $shop_id = I('shop_id');
        $shopGoodsModel =  M('shop_goods');
        $shop_goods = $shopGoodsModel->where(array('goods_id'=>$goods_id, 'shop_id'=>$shop_id))->find();

        if(!$shop_goods){
            $this->returnInfo(-1,"商品信息不存在");
        }

        if($shop_goods['shop_on_sale'] == 0){
            $this->returnInfo(-1,"商品未上架");
        }
        $store = I('store_count', 0, 'intval');
        if($store <= 0){
            $this->returnInfo(-1,"库存数量为大于0的整数");
        }

        $is_success = $shopGoodsModel->where(array('id'=>$shop_goods['id']))->setField('store_count', $store);
        if($is_success === false){
            $this->returnInfo(-1,"操作失败");
        }
        $this->returnInfo(1,"操作成功");
    }

    public function updateOnSale(){
        $goods_id = I('goods_id', 0, 'intval');
        if($goods_id <= 0){
            $this->returnInfo(-1,"商品信息有误");
        }

        $goodsInfo = M('goods')->where(array('goods_id'=>$goods_id))->find();

        if(!$goodsInfo){
            $this->returnInfo(-1,"商品信息不存在");
        }

        $shop_id = I('shop_id');
        $shopGoodsModel =  M('shop_goods');
        $shop_goods = $shopGoodsModel->where(array('goods_id'=>$goods_id, 'shop_id'=>$shop_id))->find();

        if(!$shop_goods){
            $add_data['shop_id'] = $shop_id;
            $add_data['goods_id'] = $goods_id;

            $shop_goods_id = $shopGoodsModel->add($add_data);
            if(!$shop_goods_id){
                $this->returnInfo(-1,"操作失败");
            }
        }else{
            $update_sale = $shop_goods['shop_on_sale'] == 0 ? 1 : 0;
            $is_success = $shopGoodsModel->where(array('id'=>$shop_goods['id']))->setField('shop_on_sale', $update_sale);
            if($is_success === false){
                $this->returnInfo(-1,"操作失败");
            }
        }
        $this->returnInfo(1,"操作成功");
    }

    public function addEditShopGoods()
    {

        $shopGoods = D('shop_goods'); //
        $goods_id = I('goods_id', 0, 'intval');
        //ajax提交验证
        if (IS_POST) {

            $shop_id = I('shop_id', 0, 'intval');
            $data['shop_id'] = $shop_id;
            $data['goods_id'] = $goods_id;
            $count = $shopGoods->where($data)->count();
            if($count > 0){
                $return_arr = array(
                    'status' => 0,
                    'msg' => '操作失败',
                    'data' => array('url' => ''),
                );
            }else{
                $shopgoodsid = $shopGoods->add($data); // 写入数据到数据库

                $return_arr = array(
                    'status' => 1,
                    'msg' => '操作成功',
                    'data' => array('url' => U('Admin/ShopGoods/shopGoodsList')),
                );
            }

            $this->ajaxReturn(json_encode($return_arr));
        }


        //定义一个当前页
        $p=1;
        //定义一个每页显示条数
        $page_size=10;
        $off_size=($p-1)*$page_size;
        $shop_where['shop_status'] = 2;
        $shop_where['isvalid'] = 1;
        $shop_where['goods_type'] = 0;
        $shop_ids_not = M('shop_goods')->where(array('goods_id'=>$goods_id))->getField('shop_id', true);
        //print_r($shop_ids_not);
        $shop_ids_not[] = 1;
        //print_r($shop_ids_not);die;
        //if($shop_ids_not){
            $shop_where['id'] = array('not in', $shop_ids_not);
        //}

        $shop_data_count = M('shop')->where($shop_where)->count();
        $total_page=ceil($shop_data_count/$page_size);
        //定义上一页
        $last=$p-1<1?1:$p-1;
        //定义下一页
        $next=$p+1>$total_page?$total_page:$p+1;

        //获取店铺申请列表
        $shop_data = M('shop')->where($shop_where)->limit($off_size,$page_size)->select();
        if(!$shop_data){
            $this->assign('shop_data',[]);
            $this->display();
            return false;
        }

        $this->assign('shop_data',$shop_data);
        $this->assign('last',$last);
        $this->assign('next',$next);
        $this->assign('total_page',$total_page);
        $this->assign('goods_id',  $goods_id);
        $this->assign('p',$p);

        $this->display();
    }
    /**
     * 删除商品
     */
    public function del()
    {
        $id = $_GET['id'];
        $error = '';

        $goodsInfo = M("shop_goods")->where('id ='.$id)->find();

        if( !$goodsInfo )
        {
            $this->returnInfo(-1,"渠道商品不存在");
        }

        // 判断此商品是否有订单
        //$c1 = M('OrderGoods')->where("goods_id = $goods_id")->count('1');
        //$c1 && $error .= '此商品有订单,不得删除! <br/>';


        // 商品团购
        //$c1 = M('group_buy')->where("goods_id = $goods_id")->count('1');
        //$c1 && $error .= '此商品有团购活动,不得删除! <br/>';

        // 商品退货记录
        //$c1 = M('return_goods')->where("goods_id = $goods_id")->count('1');
        //$c1 && $error .= '此商品有退货记录,不得删除! <br/>';

        if($error)
        {
            $return_arr = array('status' => -1,'msg' =>$error,'data'  =>'',);   //$return_arr = array('status' => -1,'msg' => '删除失败','data'  =>'',);
            $this->ajaxReturn(json_encode($return_arr));
        }

        // 删除此商品
        M("shop_goods")->where('id ='.$id)->delete();  //商品表

        $return_arr = array('status' => 1,'msg' => '操作成功','data'  =>'',);   //$return_arr = array('status' => -1,'msg' => '删除失败','data'  =>'',);
        $this->ajaxReturn(json_encode($return_arr));
    }
}